package com.youjiang.spirder.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.youjiang.spirder.service.CoreService;

/**
 * @Description: 核心控制层
 * @Company: yimo
 * @author: Kwum
 * @date 2018年2月24日 下午3:21:44
 */

@RestController
@RequestMapping(produces="application/json;charset=UTF-8")
public class CoreController {
	
	@Resource
	private CoreService coreService;
	
	/**
	 * 根据平台源获取平台类别
	 * @param sourceId 平台源
	 * @return
	 * @throws Exception
	 * @author Kwum
	 */
	@PostMapping("categorys")
	@ResponseBody
	public String getCategorysBySourceId(
			@RequestParam(value="sourceId", required=false, defaultValue="1")Integer sourceId) throws Exception{
		
		return coreService.selectCategoryBySourceId(sourceId);
	}
	
	/**
	 * 获取文章列表
	 * @param sourceId 平台源
	 * @param category 类别名称
	 * @param keyword 关键字
	 * @param currentPage 当前页码
	 * @param totalCount 当前页所需条目数
	 * @return
	 * @throws Exception
	 * @author Kwum
	 */
	@PostMapping("list")
	@ResponseBody
	public String getArticleList(
			@RequestParam(value="sourceId")Integer sourceId,
			@RequestParam(value="category", required=false)String category,
			@RequestParam(value="keyword", required=false)String keyword,
			@RequestParam(value="currentPage", required=false, defaultValue="1")Integer currentPage,
			@RequestParam(value="totalCount", required=false, defaultValue="10")Integer totalCount) throws Exception{
		
		return coreService.selectArticleByCategory(sourceId, category, keyword, currentPage, totalCount);
	}
	
	/**
	 * 下载文章
	 * @param resp
	 * @param sourceId 平台源
	 * @param title 标题
	 * @param url 链接
	 * @throws Exception
	 * @author Kwum
	 */
	@GetMapping("download")
	@ResponseBody
	public void downloadArtcle(HttpServletResponse resp,
			@RequestParam(value="sourceId")Integer sourceId,
			@RequestParam(value="title")String title,
			@RequestParam(value="url")String url) throws Exception{
		coreService.downloadArtcle(sourceId, title, url, resp);
	}
}
